package com.liang.Schedule;


import com.liang.myWebSocketServer.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@EnableScheduling//开启定时任务
public class noticeScheduleTask {

    //时间格式化任务器材
    private static DateTimeFormatter pattern=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Value("notice")
    private String noticeTitle;

    @Value("timer_notice")
    private String timer_notice;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private WebSocketServer webSocketServer;

//    @Scheduled(cron="*/10 * * * * ?")//每十分钟执行一次,通知为接受notice的用户
//    public void noticePulish(){
////        LocalDateTime now= LocalDateTime.now();
////        System.out.println("spring task 这是定时任务，时间是："+pattern.format(now));
//        //获取redis中存储的需要被接受的消息的id
//        Set<String> members = redisTemplate.opsForSet().members(timer_notice);
//
//        //遍历获得所有的消息接收人员存储的set的key。
//        List<String> collect = members.stream().map((noticeid) -> {
//            return noticeTitle + noticeid;
//        }).collect(Collectors.toList());
//
//        //在redis中取得key为collect中元素的并集，为获得的需要接收到消息的userid。
//        Set<String> userIdSet = redisTemplate.opsForSet().union(collect);
//
//        //遍历userIdSet发送websocket消息
//        userIdSet.stream().forEach(
//                (userid)->{
//                    long l = Long.parseLong(userid);
//                    webSocketServer.sendOneMessage(l,"你有通知未被接收!");
//                }
//        );
//    }


}